package main

import (
	"flag"
	"fmt"
	"io"
	"net"
	"os"
)

type Client struct {
	ServerIp   string
	ServerPort int
	Name       string
	conn       net.Conn
	flag       int //模式
}

func NewClient(serverIp string, serverPort int) *Client {
	//创建对象
	client := &Client{
		ServerIp:   serverIp,
		ServerPort: serverPort,
		flag:       -1,
	}
	//链接server
	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", serverIp, serverPort))
	if err != nil {
		fmt.Println("net.Dial() error:", err)
		return nil
	}

	client.conn = conn
	//返回对象
	return client
}

// 用go程处理server的消息，显示到标准输出
func (client *Client) DealResponse() {
	io.Copy(os.Stdout, client.conn) //conn有数据就拷贝到Stdout中 永久阻塞监听
	// for {
	// 	buf:=make([]byte,4096)
	// 	client.conn.Read(buf)
	// fmt.Println(buf)
	// }
}
func (client *Client) UpdateName() bool {
	fmt.Println("----请输入新用户名")
	fmt.Scanln(&client.Name)
	sendMsg := "rename|" + client.Name + "\n"
	_, err := client.conn.Write([]byte(sendMsg))
	if err != nil {
		fmt.Println("conn.Write err:", err)
		return false
	}
	return true
}
func (client *Client) SelectUsers() {
	sendMsg := "who\n"
	_, err := client.conn.Write([]byte(sendMsg))
	if err != nil {
		fmt.Println("conn.Write err:", err)
		return
	}
}
func (client *Client) PrivateChat() {
	var chatName string
	client.SelectUsers()
	fmt.Println("----输入私聊对象[用户名],exit退出")
	fmt.Scanln(&chatName)

	for chatName != "exit" {
		var sendMsg string
		fmt.Println("----请输入私聊内容,exit退出")
		fmt.Scanln(&sendMsg)
		for sendMsg != "exit" {
			if len(sendMsg) != 0 {
				msg := "to|" + chatName + "|" + sendMsg + "\n\n"
				_, err := client.conn.Write([]byte(msg))
				if err != nil {
					fmt.Println("conn.Write err:", err)
					break
				}

			}
			sendMsg = ""
			fmt.Println("----请输入私聊内容,exit退出")
			fmt.Scanln(&sendMsg)
		}
		chatName = ""
		fmt.Println("----输入私聊对象[用户名],exit退出")
		fmt.Scanln(&chatName)

	}
}
func (client *Client) PublicChat() {
	var chatMsg string
	fmt.Println("----输入你想广播的消息,exit则退出。")
	fmt.Scanln(&chatMsg)
	for chatMsg != "exit" {
		//发送内容到服务器
		if len(chatMsg) != 0 {
			sendmsg := chatMsg + "\n"
			_, err := client.conn.Write([]byte(sendmsg)) //发送数据
			if err != nil {
				fmt.Println("conn.Write err:", err)
				break
			}
		}
		chatMsg = ""
		fmt.Println("----输入你想广播的消息,exit则退出。")
		fmt.Scanln(&chatMsg)
	}
}

func (client *Client) menu() bool {
	var flag int
	fmt.Println("1.公聊模式")
	fmt.Println("2.私聊模式")
	fmt.Println("3.更新用户名")
	fmt.Println("0.退出")
	fmt.Scanln(&flag)

	if flag >= 0 && flag <= 3 {
		client.flag = flag
		return true
	} else {
		fmt.Println("———输入不合法————")
		return false
	}

}

func (client *Client) Run() {
	for client.flag != 0 {
		for !client.menu() {
		}
		switch client.flag {
		case 1:
			client.PublicChat()
			fmt.Println("--公聊模式--")
			break
		case 2:
			client.PrivateChat()
			fmt.Println("--私聊模式--")
			break
		case 3: //更新用户名
			client.UpdateName()
			fmt.Println("--更新用户名--")
			break

		}
	}
}

var serverIp string
var serverPort int

func init() {
	flag.StringVar(&serverIp, "ip", "127.0.0.1", "设置服务器IP地址(默认127.0.0.1)") //把127.0.0.1赋给ServerIp
	flag.IntVar(&serverPort, "port", 8888, "设置服务器端口(默认8888)")
}

func main() {
	//命令行解析

	flag.Parse()

	client := NewClient(serverIp, serverPort)

	if client == nil {
		fmt.Println("-------链接服务器失败\n")
		return
	}

	fmt.Println("-------链接服务器成功\n")
	go client.DealResponse() //处理server回执的消息
	//启动客户端业务
	client.Run()

}
